package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.BaseSaleAttr;
import com.atguigu.gmall.product.model.SpuImage;
import com.atguigu.gmall.product.model.SpuInfo;
import com.atguigu.gmall.product.model.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuManageService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: atguigu
 * @create: 2023-04-18 14:17
 */
@RestController
@RequestMapping("/admin/product")
public class SpuManageController {

    @Autowired
    private SpuManageService spuManageService;


    /**
     * 分页查询商品Spu列表
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/{page}/{size}")
    public Result getSpuListByPage(@PathVariable("page") Long page,
                                   @PathVariable("size") Long size,
                                   @RequestParam(value = "category3Id") Long category3Id) {
        //1.封装分页对象Page
        Page<SpuInfo> iPage = new Page<>(page, size);
        //2.调用业务层完成分页查询
        iPage = spuManageService.getSpuListByPage(iPage, category3Id);
        return Result.ok(iPage);
    }


    /**
     * 查询所有销售属性列表
     *
     * @return
     */
    @GetMapping("/baseSaleAttrList")
    public Result getBaseSaleAttrList() {
        List<BaseSaleAttr> list = spuManageService.getBaseSaleAttrList();
        return Result.ok(list);
    }


    /**
     * 保存商品SPU信息
     *
     * @param spuInfo
     * @return
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        spuManageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }


    /**
     * 根据商品SPUID查询商品SPU所有销售属性-包含销售属性值
     *
     * @param spuId
     * @return
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable("spuId") Long spuId) {
        List<SpuSaleAttr> list = spuManageService.getSpuSaleAttrList(spuId);
        return Result.ok(list);
    }


    /**
     * 根据商品SPUID查询商品所有图片
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable("spuId") Long spuId){
        List<SpuImage> list = spuManageService.getSpuImageList(spuId);
        return Result.ok(list);
    }
}
